package be.jaisy.osgi.console.ui.equinox;

import java.util.Collection;

import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import be.jaisy.osgi.console.service.api.IBundleManagementService;
import be.jaisy.osgi.console.service.api.ILogReaderService;
import be.jaisy.osgi.console.service.api.IOsgiServiceManagementService;
import be.jaisy.osgi.console.service.api.to.BundleTo;
import be.jaisy.osgi.console.service.api.to.LogEntryTo;
import be.jaisy.osgi.console.service.api.to.OsgiServiceTo;

public class ProjectCommandProvider implements CommandProvider {

	private BundleContext context;

	public ProjectCommandProvider(BundleContext context) {
		super();
		this.context = context;
	}

	public String getHelp() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("\n\n---Controlling the OsgiConsole services---\n");
		buffer.append("\tlistBundle (lb) - list bundle service\n");
		buffer.append("\tgetBundle (lb) - get bundle service\n");
		buffer.append("\tlistLog (ll) - list log service\n");
		buffer.append("\tlistOsgiService (ls) - list Osgi Service service\n");
		return buffer.toString();

	}

	public void _lb(CommandInterpreter intp) throws Exception {
		_listBundle(intp);
	}

	@SuppressWarnings("unchecked")
	public void _listBundle(CommandInterpreter intp) throws Exception {

		ServiceReference rf = context.getServiceReference(IBundleManagementService.class.getName());
		if (rf == null) {
			System.err.println("serviceReference for [" + IBundleManagementService.class.getName() + "] not found");
			return;
		}
		IBundleManagementService service = (IBundleManagementService) context.getService(rf);

		Collection<BundleTo> bundles = service.listBundle();
		System.out.println("listBundle return " + bundles.size() + " bundle:");
		for (BundleTo to : bundles) {
			System.out.println(to.toString());
		}
	}

	public void _gb(CommandInterpreter intp) throws Exception {
		_getBundle(intp);
	}

	@SuppressWarnings("unchecked")
	public void _getBundle(CommandInterpreter intp) throws Exception {

		ServiceReference rf = context.getServiceReference(IBundleManagementService.class.getName());
		if (rf == null) {
			System.err.println("serviceReference for [" + IBundleManagementService.class.getName() + "] not found");
			return;
		}
		IBundleManagementService service = (IBundleManagementService) context.getService(rf);
		String id = intp.nextArgument();
		BundleTo bundle = service.getBundle(new Long(id));
		if (bundle == null)
			System.out.println("Bundle [" + id + "] not found");
		else
			System.out.println("getBundle return " + bundle.toString());
	}

	public void _ll(CommandInterpreter intp) throws Exception {
		_listLog(intp);
	}

	@SuppressWarnings("unchecked")
	public void _listLog(CommandInterpreter intp) throws Exception {

		ServiceReference rf = context.getServiceReference(ILogReaderService.class.getName());
		if (rf == null) {
			System.err.println("serviceReference for [" + ILogReaderService.class.getName() + "] not found");
			return;
		}
		ILogReaderService service = (ILogReaderService) context.getService(rf);

		Collection<LogEntryTo> logEntries = service.listLog();
		System.out.println("listLog return " + logEntries.size() + " logEntries:");
		for (LogEntryTo to : logEntries) {
			System.out.println(to.toString());
		}
		if (logEntries != null && logEntries.size() > 0)
			lastLogEntryTo = logEntries.iterator().next();
	}

	LogEntryTo lastLogEntryTo = null;

	public void _lnl(CommandInterpreter intp) throws Exception {
		_listNewLog(intp);
	}

	@SuppressWarnings("unchecked")
	public void _listNewLog(CommandInterpreter intp) throws Exception {

		ServiceReference rf = context.getServiceReference(ILogReaderService.class.getName());
		if (rf == null) {
			System.err.println("serviceReference for [" + ILogReaderService.class.getName() + "] not found");
			return;
		}
		ILogReaderService service = (ILogReaderService) context.getService(rf);

		Collection<LogEntryTo> newLogEntries = service.listNewLog(lastLogEntryTo);
		System.out.println("listNewLog return " + newLogEntries.size() + " logEntries:");
		for (LogEntryTo to : newLogEntries) {
			System.out.println(to.toString());
		}
		if (newLogEntries != null && newLogEntries.size() > 0)
			lastLogEntryTo = newLogEntries.iterator().next();

	}

	public void _ls(CommandInterpreter intp) throws Exception {
		_listOsgiService(intp);
	}

	@SuppressWarnings("unchecked")
	public void _listOsgiService(CommandInterpreter intp) throws Exception {

		ServiceReference rf = context.getServiceReference(IOsgiServiceManagementService.class.getName());
		if (rf == null) {
			System.err.println("serviceReference for [" + IBundleManagementService.class.getName() + "] not found");
			return;
		}
		IOsgiServiceManagementService service = (IOsgiServiceManagementService) context.getService(rf);

		Collection<OsgiServiceTo> osgiServices = service.listRegisteredServices();
		System.out.println("listOsgiService return " + osgiServices.size() + " osgi service:");
		for (OsgiServiceTo to : osgiServices) {
			System.out.println(to.toString());
		}
	}
}